package com.yuan.zs.ui;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.alipay.sdk.app.PayTask;
import com.yuan.base.framework.BaseFrameworkFragment;
import com.yuan.base.framework.BaseUserManager;
import com.yuan.base.framework.BaseViewHolderAdapter;
import com.yuan.base.framework.bean.UserInfo;
import com.yuan.base.framework.http.BaseHttpCallBack;
import com.yuan.base.framework.http.BaseHttpRequest;
import com.yuan.base.framework.image.BaseImageLoad;
import com.yuan.base.utils.LogUtil;
import com.yuan.base.widget.inlist.NoScrollListView;
import com.yuan.zs.R;
import com.yuan.zs.Urls;
import com.yuan.zs.bean.Child;
import com.yuan.zs.common.CacheManager;
import com.yuan.zs.ui.account.AccountManageActivity;
import com.yuan.zs.ui.account.ApplyApprovalActivity;
import com.yuan.zs.ui.account.ApplyChildBindingActivity;
import com.yuan.zs.ui.me.ChildInfoActivity;
import com.yuan.zs.ui.me.ParentInfoActivity;
import com.yuan.zs.ui.me.SettingActivity;

import java.util.HashMap;
import java.util.Map;

/**
 * 我的
 */
public class MainMyFragment extends BaseFrameworkFragment {

	// --------------------------控件--------------------------
	private TextView tv_user_name;
	private TextView tv_user_phone;
	private ImageView iv_user_avatar;
	private NoScrollListView nlv_child_list;
	// --------------------------变量--------------------------

	@Override
	protected int getLayoutId() {
		return R.layout.fragment_main_my;
	}

	@Override
	protected void bindViews() {
		tv_user_name = findViewById(R.id.tv_user_name);
		tv_user_phone = findViewById(R.id.tv_user_phone);
		iv_user_avatar = findViewById(R.id.iv_user_avatar);
		nlv_child_list = findViewById(R.id.nlv_child_list);
		iv_user_avatar.setOnClickListener(v -> startActivity(new Intent(context, ParentInfoActivity.class)));

		setOnClickListener(R.id.rl_setting, v -> startActivity(new Intent(context, SettingActivity.class)));
		setOnClickListener(R.id.ll_to_bind_child, v -> startActivity(new Intent(context, ApplyChildBindingActivity.class)));
		setOnClickListener(R.id.ll_to_bind_apply_list, v -> startActivity(new Intent(context, ApplyApprovalActivity.class)));
		setOnClickListener(R.id.ll_do_chongzhi, v -> {
			HashMap<String, Object> params = new HashMap<>();
			params.put("subject", "测试ll");
			params.put("body", "0.01");
			params.put("price", "0.02");
//			params.put("outTradeNo", "b05fab8f305f4527a1edd7fc850c0413");
			BaseHttpRequest.post(Urls.getUrl("/api/v1/alipay/payorder?subject=充值&body=测试充值&price=0.02"), params, new BaseHttpCallBack<JSONObject>() {

				@Override
				public void onSuccess(JSONObject result) {
					if (result != null) {
						final String orderInfo = result.getString("orderInfo");   // 订单信息
						Runnable payRunnable = () -> {
							PayTask alipay = new PayTask(context);
							Map<String, String> result1 = alipay.payV2(orderInfo, true);
							//{"alipay_trade_app_pay_response":{"code":"10000","msg":"Success","app_id":"2018092461532247","auth_app_id":"2018092461532247","charset":"utf-8","timestamp":"2019-01-17 00:39:19","out_trade_no":"5ec6eb70b93a4fb3a48d10358db421bf","total_amount":"0.01","trade_no":"2019011722001463781012750690","seller_id":"2088231878795554"},"sign":"eBHvSDxVHR+uZ4+D0qsmTSjeNGavjxKP6MRdk4UMu6DbG1TKLPVdmRLTuVGpd05wWJgmusoN763TodelmYDJ/orN8HSWgzJTQp8T74pF7IA2VA7cGW/XhAXhfxa0VwiXJhs2eLlum5+Gz0jRzJDVIYk8xzpheTkXe2hQgNPxUBoWyDccgUvlqpW9tS3yPn7M7OXg6C3GUSexCVz2VfuzxPV2LnXKWEP4wIAC8zGoBhTE9V4JAyHD0PJEF9pzC4eVvpAxluc3e+E6EWYto/yp5na3z67uzMVuYxNAdUKGDgZzpw1SineCbl+ZPNUx3wH/38HaHGxYlWBXEC/93WDd0A==","sign_type":"RSA2"}
							context.runOnUiThread(()->{
								LogUtil.d(result1);
							});
						};
						// 必须异步调用
						Thread payThread = new Thread(payRunnable);
						payThread.start();
					}
				}

			});
		});
		nlv_child_list.setOnItemClickListener((parent, view, position, id) -> {
			Intent intent = new Intent(context, ChildInfoActivity.class);
			intent.putExtra("child", (Child) parent.getItemAtPosition(position));
			startActivity(intent);
		});
		setOnClickListener(R.id.ll_to_account_manage, v -> startActivity(new Intent(context, AccountManageActivity.class)));
	}

	@Override
	protected void initUI() {
		UserInfo user = BaseUserManager.getUser();
		tv_user_name.setText(user.getName());
		tv_user_phone.setText(user.getPhone1());
		BaseImageLoad.loadCircularAvatar(user.getSpic(), iv_user_avatar);
	}

	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
	}

	@Override
	public void onResume() {
		super.onResume();
		initUI();
	}


	@Override
	public void onStart() {
		super.onStart();
		refreshChildData();
	}

	private void refreshChildData() {
		ChildAdapter adapter = new ChildAdapter(context);
		adapter.addListData(CacheManager.getInstance().getChildList());
		nlv_child_list.setAdapter(adapter);
		if (adapter.getCount() == 0) {
			((View) nlv_child_list.getParent()).setVisibility(View.GONE);
		} else {
			((View) nlv_child_list.getParent()).setVisibility(View.VISIBLE);
		}
	}

	private class ChildAdapter extends BaseViewHolderAdapter<Child, ChildHolder> {

		ChildAdapter(Context context) {
			super(context);
		}

		@Override
		protected void bindViews(View convertView, ChildHolder holder, Child item) {
			holder.tv_child_name = convertView.findViewById(R.id.tv_child_name);
			holder.tv_child_sex = convertView.findViewById(R.id.tv_child_sex);
		}

		@Override
		protected View getConvertView(Child item, LayoutInflater inflater, int position, ViewGroup parent) {
			return inflater.inflate(R.layout.item_child_list, parent, false);
		}

		@Override
		protected ChildHolder getHolder() {
			return new ChildHolder();
		}

		@Override
		protected void refreshView(int position, Child item, View convertView, ChildHolder holder) {
			holder.tv_child_name.setText(item.getName());
			holder.tv_child_sex.setText(item.getGender());
		}
	}

	private class ChildHolder {
		TextView tv_child_name;
		TextView tv_child_sex;
	}
}
